/*
 * Author: Honrun
 */
#include "stm32f4xx_hal.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

#include "DriverNetwork.h"
#include "DevicesQueue.h"
#include "DevicesFlash.h"
#include "DevicesDisk.h"

//#include "bootloader.h"
#include "taskMessageRead.h"






const static uint8_t ucMBRdata[512] = {
    0x33, 0xC0, 0x8E, 0xD0, 0xBC, 0x00, 0x7C, 0xFB, 0x50, 0x07, 0x50, 0x1F, 0xFC, 0xBE, 0x1B, 0x7C,
    0xBF, 0x1B, 0x06, 0x50, 0x57, 0xB9, 0xE5, 0x01, 0xF3, 0xA4, 0xCB, 0xBD, 0xBE, 0x07, 0xB1, 0x04,
    0x38, 0x6E, 0x00, 0x7C, 0x09, 0x75, 0x13, 0x83, 0xC5, 0x10, 0xE2, 0xF4, 0xCD, 0x18, 0x8B, 0xF5,
    0x83, 0xC6, 0x10, 0x49, 0x74, 0x19, 0x38, 0x2C, 0x74, 0xF6, 0xA0, 0xB5, 0x07, 0xB4, 0x07, 0x8B,
    0xF0, 0xAC, 0x3C, 0x00, 0x74, 0xFC, 0xBB, 0x07, 0x00, 0xB4, 0x0E, 0xCD, 0x10, 0xEB, 0xF2, 0x88,
    0x4E, 0x10, 0xE8, 0x46, 0x00, 0x73, 0x2A, 0xFE, 0x46, 0x10, 0x80, 0x7E, 0x04, 0x0B, 0x74, 0x0B,
    0x80, 0x7E, 0x04, 0x0C, 0x74, 0x05, 0xA0, 0xB6, 0x07, 0x75, 0xD2, 0x80, 0x46, 0x02, 0x06, 0x83,
    0x46, 0x08, 0x06, 0x83, 0x56, 0x0A, 0x00, 0xE8, 0x21, 0x00, 0x73, 0x05, 0xA0, 0xB6, 0x07, 0xEB,
    0xBC, 0x81, 0x3E, 0xFE, 0x7D, 0x55, 0xAA, 0x74, 0x0B, 0x80, 0x7E, 0x10, 0x00, 0x74, 0xC8, 0xA0,
    0xB7, 0x07, 0xEB, 0xA9, 0x8B, 0xFC, 0x1E, 0x57, 0x8B, 0xF5, 0xCB, 0xBF, 0x05, 0x00, 0x8A, 0x56,
    0x00, 0xB4, 0x08, 0xCD, 0x13, 0x72, 0x23, 0x8A, 0xC1, 0x24, 0x3F, 0x98, 0x8A, 0xDE, 0x8A, 0xFC,
    0x43, 0xF7, 0xE3, 0x8B, 0xD1, 0x86, 0xD6, 0xB1, 0x06, 0xD2, 0xEE, 0x42, 0xF7, 0xE2, 0x39, 0x56,
    0x0A, 0x77, 0x23, 0x72, 0x05, 0x39, 0x46, 0x08, 0x73, 0x1C, 0xB8, 0x01, 0x02, 0xBB, 0x00, 0x7C,
    0x8B, 0x4E, 0x02, 0x8B, 0x56, 0x00, 0xCD, 0x13, 0x73, 0x51, 0x4F, 0x74, 0x4E, 0x32, 0xE4, 0x8A,
    0x56, 0x00, 0xCD, 0x13, 0xEB, 0xE4, 0x8A, 0x56, 0x00, 0x60, 0xBB, 0xAA, 0x55, 0xB4, 0x41, 0xCD,
    0x13, 0x72, 0x36, 0x81, 0xFB, 0x55, 0xAA, 0x75, 0x30, 0xF6, 0xC1, 0x01, 0x74, 0x2B, 0x61, 0x60,
    0x6A, 0x00, 0x6A, 0x00, 0xFF, 0x76, 0x0A, 0xFF, 0x76, 0x08, 0x6A, 0x00, 0x68, 0x00, 0x7C, 0x6A,
    0x01, 0x6A, 0x10, 0xB4, 0x42, 0x8B, 0xF4, 0xCD, 0x13, 0x61, 0x61, 0x73, 0x0E, 0x4F, 0x74, 0x0B,
    0x32, 0xE4, 0x8A, 0x56, 0x00, 0xCD, 0x13, 0xEB, 0xD6, 0x61, 0xF9, 0xC3, 0x49, 0x6E, 0x76, 0x61,
    0x6C, 0x69, 0x64, 0x20, 0x70, 0x61, 0x72, 0x74, 0x69, 0x74, 0x69, 0x6F, 0x6E, 0x20, 0x74, 0x61,
    0x62, 0x6C, 0x65, 0x00, 0x45, 0x72, 0x72, 0x6F, 0x72, 0x20, 0x6C, 0x6F, 0x61, 0x64, 0x69, 0x6E,
    0x67, 0x20, 0x6F, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6E, 0x67, 0x20, 0x73, 0x79, 0x73, 0x74,
    0x65, 0x6D, 0x00, 0x4D, 0x69, 0x73, 0x73, 0x69, 0x6E, 0x67, 0x20, 0x6F, 0x70, 0x65, 0x72, 0x61,
    0x74, 0x69, 0x6E, 0x67, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0xC4, 0x4F, 0xA5, 0x00, 0x00, 0x80, 0x20,
    0x21, 0x00, 0x06, 0xFE, 0x3F, 0x81, 0x00, 0x08, 0x00, 0x00, 0x00, 0xF8, 0x1F, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA
};

const static uint8_t ucDBRdata[512] = {
    0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00, 0x02, 0x20, 0x08, 0x00,
    0x02, 0x00, 0x02, 0x00, 0x00, 0xF8, 0x00, 0x01, 0x3F, 0x00, 0x40, 0x00, 0x00, 0x08, 0x00, 0x00,
    0x00, 0xF8, 0x1F, 0x00, 0x80, 0x00, 0x29, 0x1E, 0x6A, 0x4C, 0x6E, 0x4E, 0x4F, 0x20, 0x4E, 0x41,
    0x4D, 0x45, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41, 0x54, 0x31, 0x36, 0x20, 0x20, 0x20, 0x33, 0xC9,
    0x8E, 0xD1, 0xBC, 0xF0, 0x7B, 0x8E, 0xD9, 0xB8, 0x00, 0x20, 0x8E, 0xC0, 0xFC, 0xBD, 0x00, 0x7C,
    0x38, 0x4E, 0x24, 0x7D, 0x24, 0x8B, 0xC1, 0x99, 0xE8, 0x3C, 0x01, 0x72, 0x1C, 0x83, 0xEB, 0x3A,
    0x66, 0xA1, 0x1C, 0x7C, 0x26, 0x66, 0x3B, 0x07, 0x26, 0x8A, 0x57, 0xFC, 0x75, 0x06, 0x80, 0xCA,
    0x02, 0x88, 0x56, 0x02, 0x80, 0xC3, 0x10, 0x73, 0xEB, 0x33, 0xC9, 0x8A, 0x46, 0x10, 0x98, 0xF7,
    0x66, 0x16, 0x03, 0x46, 0x1C, 0x13, 0x56, 0x1E, 0x03, 0x46, 0x0E, 0x13, 0xD1, 0x8B, 0x76, 0x11,
    0x60, 0x89, 0x46, 0xFC, 0x89, 0x56, 0xFE, 0xB8, 0x20, 0x00, 0xF7, 0xE6, 0x8B, 0x5E, 0x0B, 0x03,
    0xC3, 0x48, 0xF7, 0xF3, 0x01, 0x46, 0xFC, 0x11, 0x4E, 0xFE, 0x61, 0xBF, 0x00, 0x00, 0xE8, 0xE6,
    0x00, 0x72, 0x39, 0x26, 0x38, 0x2D, 0x74, 0x17, 0x60, 0xB1, 0x0B, 0xBE, 0xA1, 0x7D, 0xF3, 0xA6,
    0x61, 0x74, 0x32, 0x4E, 0x74, 0x09, 0x83, 0xC7, 0x20, 0x3B, 0xFB, 0x72, 0xE6, 0xEB, 0xDC, 0xA0,
    0xFB, 0x7D, 0xB4, 0x7D, 0x8B, 0xF0, 0xAC, 0x98, 0x40, 0x74, 0x0C, 0x48, 0x74, 0x13, 0xB4, 0x0E,
    0xBB, 0x07, 0x00, 0xCD, 0x10, 0xEB, 0xEF, 0xA0, 0xFD, 0x7D, 0xEB, 0xE6, 0xA0, 0xFC, 0x7D, 0xEB,
    0xE1, 0xCD, 0x16, 0xCD, 0x19, 0x26, 0x8B, 0x55, 0x1A, 0x52, 0xB0, 0x01, 0xBB, 0x00, 0x00, 0xE8,
    0x3B, 0x00, 0x72, 0xE8, 0x5B, 0x8A, 0x56, 0x24, 0xBE, 0x0B, 0x7C, 0x8B, 0xFC, 0xC7, 0x46, 0xF0,
    0x3D, 0x7D, 0xC7, 0x46, 0xF4, 0x29, 0x7D, 0x8C, 0xD9, 0x89, 0x4E, 0xF2, 0x89, 0x4E, 0xF6, 0xC6,
    0x06, 0x96, 0x7D, 0xCB, 0xEA, 0x03, 0x00, 0x00, 0x20, 0x0F, 0xB6, 0xC8, 0x66, 0x8B, 0x46, 0xF8,
    0x66, 0x03, 0x46, 0x1C, 0x66, 0x8B, 0xD0, 0x66, 0xC1, 0xEA, 0x10, 0xEB, 0x5E, 0x0F, 0xB6, 0xC8,
    0x4A, 0x4A, 0x8A, 0x46, 0x0D, 0x32, 0xE4, 0xF7, 0xE2, 0x03, 0x46, 0xFC, 0x13, 0x56, 0xFE, 0xEB,
    0x4A, 0x52, 0x50, 0x06, 0x53, 0x6A, 0x01, 0x6A, 0x10, 0x91, 0x8B, 0x46, 0x18, 0x96, 0x92, 0x33,
    0xD2, 0xF7, 0xF6, 0x91, 0xF7, 0xF6, 0x42, 0x87, 0xCA, 0xF7, 0x76, 0x1A, 0x8A, 0xF2, 0x8A, 0xE8,
    0xC0, 0xCC, 0x02, 0x0A, 0xCC, 0xB8, 0x01, 0x02, 0x80, 0x7E, 0x02, 0x0E, 0x75, 0x04, 0xB4, 0x42,
    0x8B, 0xF4, 0x8A, 0x56, 0x24, 0xCD, 0x13, 0x61, 0x61, 0x72, 0x0B, 0x40, 0x75, 0x01, 0x42, 0x03,
    0x5E, 0x0B, 0x49, 0x75, 0x06, 0xF8, 0xC3, 0x41, 0xBB, 0x00, 0x00, 0x60, 0x66, 0x6A, 0x00, 0xEB,
    0xB0, 0x4E, 0x54, 0x4C, 0x44, 0x52, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0D, 0x0A, 0x52, 0x65,
    0x6D, 0x6F, 0x76, 0x65, 0x20, 0x64, 0x69, 0x73, 0x6B, 0x73, 0x20, 0x6F, 0x72, 0x20, 0x6F, 0x74,
    0x68, 0x65, 0x72, 0x20, 0x6D, 0x65, 0x64, 0x69, 0x61, 0x2E, 0xFF, 0x0D, 0x0A, 0x44, 0x69, 0x73,
    0x6B, 0x20, 0x65, 0x72, 0x72, 0x6F, 0x72, 0xFF, 0x0D, 0x0A, 0x50, 0x72, 0x65, 0x73, 0x73, 0x20,
    0x61, 0x6E, 0x79, 0x20, 0x6B, 0x65, 0x79, 0x20, 0x74, 0x6F, 0x20, 0x72, 0x65, 0x73, 0x74, 0x61,
    0x72, 0x74, 0x0D, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAC, 0xCB, 0xD8, 0x55, 0xAA
};

const static uint8_t ucFATdata[] = {
    0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00,
    0x09, 0x00, 0x0A, 0x00, 0x0B, 0x00, 0x0C, 0x00, 0x0D, 0x00, 0x0E, 0x00, 0x0F, 0x00, 0x10, 0x00,
    0x11, 0x00, 0x12, 0x00, 0x13, 0x00, 0x14, 0x00, 0x15, 0x00, 0x16, 0x00, 0x17, 0x00, 0x18, 0x00,
    0x19, 0x00, 0x1A, 0x00, 0x1B, 0x00, 0x1C, 0x00, 0x1D, 0x00, 0x1E, 0x00, 0x1F, 0x00, 0x20, 0x00,
    0x21, 0x00, 0x22, 0x00, 0x23, 0x00, 0x24, 0x00, 0x25, 0x00, 0x26, 0x00, 0x27, 0x00, 0x28, 0x00,
    0x29, 0x00, 0x2A, 0x00, 0x2B, 0x00, 0x2C, 0x00, 0x2D, 0x00, 0x2E, 0x00, 0x2F, 0x00, 0x30, 0x00,
    0x31, 0x00, 0x32, 0x00, 0x33, 0x00, 0x34, 0x00, 0x35, 0x00, 0x36, 0x00, 0x37, 0x00, 0x38, 0x00,
    0x39, 0x00, 0x3A, 0x00, 0x3B, 0x00, 0x3C, 0x00, 0x3D, 0x00, 0x3E, 0x00, 0x3F, 0x00, 0x40, 0x00,
    0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static uint8_t ucDIRdata[512] = {
    0x43, 0x75, 0x72, 0x72, 0x65, 0x6E, 0x74, 0x20, 0x20, 0x20, 0x20, 0x08, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0x56, 0xA8, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x20, 0x20, 0x20, 0x20, 0x20, 0x52, 0x20, 0x20, 0x54, 0x58, 0x54, 0x20, 0x18, 0xBA, 0xAF, 0x56,
    0xA8, 0x52, 0xA8, 0x52, 0x00, 0x00, 0xE3, 0x55, 0xA8, 0x52, 0x02, 0x00, 0x00, 0x40, 0x00, 0x00,
    0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x54, 0x58, 0x54, 0x20, 0x18, 0x67, 0xB2, 0x56,
    0xA8, 0x52, 0xA8, 0x52, 0x00, 0x00, 0x9A, 0x56, 0xA8, 0x52, 0x03, 0x00, 0x24, 0x01, 0x00, 0x00,
    0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x42, 0x49, 0x4E, 0x20, 0x18, 0x10, 0xB4, 0x56,
    0xA8, 0x52, 0xA8, 0x52, 0x00, 0x00, 0x8F, 0x58, 0x31, 0x51, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00
};


const static char ucReadmeFiledata[] = "\
升级说明：\r\n\r\n\
\
1、向下位机发送指令，设置下位机进入升级 bootloader 模式；\r\n\
2、请把二进制固件 \"boot.bin\" 复制到当前盘符；\r\n\
3、向下位机发送指令，重启下位机；\r\n\
4、下位机重启，升级完成。\r\n\r\n\
\
注意：\r\n\
如果升级的二进制固件有错误，重启后将导致系统不能正常开机，只能使用烧录工具进行线刷！\r\n\
";











#define DISK_NAME "Current"
/* 文件名必须全大写 */
#define DISK_CMD_FILE_NAME "DRIVER"
#define DISK_README_FILE_NAME "README"
#define DISK_DATA_FILE_NAME "BOOT"

void vDiskInit(void)
{
    memcpy(&ucDIRdata[0], DISK_NAME, strlen(DISK_NAME));
    memcpy(&ucDIRdata[32], DISK_CMD_FILE_NAME, strlen(DISK_CMD_FILE_NAME));
    memcpy(&ucDIRdata[64], DISK_README_FILE_NAME, strlen(DISK_README_FILE_NAME));
    memcpy(&ucDIRdata[96], DISK_DATA_FILE_NAME, strlen(DISK_DATA_FILE_NAME));
}




int8_t cDiskRead(uint32_t uiOffset, uint8_t *pucBuff, uint32_t uiLength)
{
    volatile static int32_t iOldLength = 0;
    int8_t cError = 0;

    /* 必须清零，以清除上次读取的遗留缓存数据 */
    memset(pucBuff, 0, uiLength);

    if((uiOffset >= DISK_CMD_ADDR) && (uiOffset < DISK_CMD_ADDR + DISK_CMD_SIZE))
    {
        /* 防止第一个区块数据不够一个簇大小，从而导致把数据填充到后面的区块 */
        iOldLength = (uiOffset == DISK_CMD_ADDR) ? iQueueGetLengthOfOccupyNeed(&g_TypeQueueUSBSend, FRAME_SEPARETOR) : iOldLength;

        /* BLOCK还有数据 */
        if(iOldLength > 0)
        {
            uiLength = ((uiLength > iOldLength) ? iOldLength : uiLength);

            /* 读取FIFO数据到USB */
            cError = enumQueuePopDatasNeed(&g_TypeQueueUSBSend, pucBuff, uiLength, FRAME_SEPARETOR);

            iOldLength -= uiLength;
        }
    }
//    else if((uiOffset >= DISK_DATA_ADDR) && (uiOffset < DISK_DATA_ADDR + DISK_DATA_SIZE))
//        cError = cFlashReadDatas(uiOffset - DISK_DATA_ADDR + BOOTLOADER_ADDR, pucBuff, uiLength);

    else if((uiOffset >= DISK_README_ADDR) && (uiOffset < DISK_README_ADDR + DISK_README_SIZE))
        memcpy(pucBuff, ucReadmeFiledata, sizeof(ucReadmeFiledata));

    else if((uiOffset >= DISK_DIR_ADDR) && (uiOffset < DISK_DIR_ADDR + DISK_DIR_SIZE))
        memcpy(pucBuff, ucDIRdata, sizeof(ucDIRdata));

    else if((uiOffset >= DISK_FAT2_ADDR) && (uiOffset < DISK_FAT2_ADDR + DISK_FAT_SIZE))
        memcpy(pucBuff, ucFATdata, sizeof(ucFATdata));

    else if((uiOffset >= DISK_FAT1_ADDR) && (uiOffset < DISK_FAT1_ADDR + DISK_FAT_SIZE))
        memcpy(pucBuff, ucFATdata, sizeof(ucFATdata));

    else if((uiOffset >= DISK_DBR_ADDR) && (uiOffset < DISK_DBR_ADDR + DISK_DBR_SIZE))
        memcpy(pucBuff, ucDBRdata, sizeof(ucDBRdata));

    else if((uiOffset >= DISK_MBR_ADDR) && (uiOffset < DISK_MBR_ADDR + DISK_MBR_SIZE))
        memcpy(pucBuff, ucMBRdata, sizeof(ucMBRdata));

    return cError;
}


int8_t cDiskWrite(uint32_t uiOffset, uint8_t *pucBuff, uint32_t uiLength)
{
    uint32_t uiCLVCmd = 0, uiCLVLength = 0, iLengthNow = 0;
    int8_t cError = 0;

    /* USB用户数据传输 */
    if((uiOffset >= DISK_CMD_ADDR) && (uiOffset < DISK_CMD_ADDR + DISK_CMD_SIZE))
    {
        /* 判断长度 */
        // iLengthNow = strnlen((char *)pucBuff, uiLength);
        iLengthNow = strlen((char *)pucBuff);
        iLengthNow = (iLengthNow > uiLength) ? uiLength : iLengthNow;

        cError = enumQueuePushDatas(&g_TypeQueueUSBRead, pucBuff, iLengthNow);

        /* 启动任务执行解析 */
        vTaskNotifyGiveFromISR(g_TaskMessageReadHand, NULL);
    }
    // /* 更新Bootloader */
    // else if((uiOffset >= DISK_DATA_ADDR) && (uiOffset < DISK_DATA_ADDR + DISK_DATA_SIZE))
    // {
    //     /* 解锁状态才可更新 */
    //     if(ucBootloaderGetState() == OTA_UPDATA_UNLOCK)
    //     {
    //         cError = cFlashWriteDatas(uiOffset - DISK_DATA_ADDR + BOOTLOADER_ADDR, pucBuff, uiLength);
    //         vLedRevesal(LED_VI_YELLOW | LED_YELLOW);
    //     }
    //     else
    //         vLedRevesal(LED_ALL);
    // }

    return cError;
}
